//
//  YHMachineManagementCallbackViewController.m
//  YHApp
//
//  Created by -- on 2021/8/8.
//

#import "HAMachineCallbackVC.h"
#import "HAMachinePullDownMachinesPopView.h"
#import "HAMachinePullDownAgentPopView.h"
#import "HAAllMachineListMo.h"
#import "HAHoemAgentListModel.h"
#import "HATermModel.h"

@interface HAMachineCallbackVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;

/***/
@property (nonatomic, strong) HAHoemAgentListModel *selAgentModel;
/***/
@property (nonatomic, strong) NSMutableArray *termList;

@end

@implementation HAMachineCallbackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"机具回拨";

    [self initUI];
    [self loadData];
    
    // Do any additional setup after loading the view.
}

- (void)initUI{
    
    UIButton *bBtn = [[UIButton alloc]init];
    [bBtn setTitle:@"确认拔发" forState:UIControlStateNormal];
    [bBtn setTitleColor:[UIColor colorWithHexString:@"@ffffff"] forState:UIControlStateNormal];
    bBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    bBtn.backgroundColor = [UIColor colorWithHexString:@"#191919"];
    [bBtn addTarget:self action:@selector(bBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bBtn];
    [bBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_offset(54);
    }];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-54);
    }];
}

-(void)loadData {
    
    [self.dataArray removeAllObjects];
    [self.dataArray addObject:@"回拨代理"];
    [self.dataArray addObject:@"选择机具"];
    [self.tableView reloadData];

}

-(void)loadNewData:(NSString *)agent_id {
    WS(weakSelf)

    NSDictionary *dic = @{
        @"agent_id":agent_id?:@"",
    };
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"sns/rebackterms"];
    request1.requestMethod = SYRequestMethodGET;
    request1.parms = dic;
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [self.termList removeAllObjects];
//                if ([[dataModel.data allKeys]containsObject:@"term"]) {
//
//                }
                NSArray *arr = [HATermModel mj_objectArrayWithKeyValuesArray:dataModel.data];
                [self.termList addObjectsFromArray:arr];
                
                HAMachinePullDownMachinesPopView *menu = [[HAMachinePullDownMachinesPopView alloc]initWithArray:self.termList];
                [menu show];
                menu.HAMachinePullDownMachinesMenuSureBlock = ^{
                    [weakSelf.tableView reloadData];
                };

            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
            [self.tableView reloadData];
        }
    }];
    
}


-(void)bBtnClick {
    
    [self.view endEditing:YES];

    if (!self.selAgentModel) {
        [MBProgressHUD showError:@"请选择代理"];
        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.selAgentModel.ID?:@"" forKey:@"agent_id"];
    [dic setValue:@"" forKey:@"source_id"];
    [dic setValue:@"" forKey:@"policy_id"];

    NSString *code = @"";
    for (HATermModel *mo in self.termList) {
        if (mo.isSel) {
            if ([NSString isEmptyString:code]) {
                code = [NSString stringWithFormat:@"%@",mo.value];
            }else {
                code = [NSString stringWithFormat:@"%@,%@",code,mo.value];
            }
        }
    }
    if ([NSString isEmptyString:code]) {
        [MBProgressHUD showError:@"请选择机具"];
        return;
    }
    [dic setValue:code forKey:@"code"];

    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodPOST;
    request.requestUrl = [NSString stringWithFormat:@"sns/reback.html"];
    request.parms = dic;
    [MBProgressHUD showLoading:self.view];

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:@"机具拨发成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMachineManagementCallbackCellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMachineManagementCallbackCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([title isEqualToString:@"回拨代理"]) {
        NSString *content = @"";
        if (self.selAgentModel) {
            content = self.selAgentModel.name;
        }else {
            content = @"请选择代理";
        }
        [self setCellConWith:cell.contentView title:title content:content isLine:YES isRImg:YES];
    }else if ([title isEqualToString:@"选择机具"]) {
        int count = NO;
        for (int i = 0; i < self.termList.count; i ++) {
            HATermModel *mo = self.termList[i];
            if (mo.isSel) {
                count += 1;
            }
        }
        NSString *content = [NSString stringWithFormat:@"已经选机具%d个",count];
        [self setCellConWith:cell.contentView title:title content:content isLine:YES isRImg:YES];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    WS(weakSelf)
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"回拨代理"]) {
        HAMachinePullDownAgentPopView *menu = [[HAMachinePullDownAgentPopView alloc]initWithTitle:title];
        [menu show];
        menu.HAMachinePullDownAgentPopSureBlock = ^(HAHoemAgentListModel * _Nonnull model) {
            weakSelf.selAgentModel = model;
            for (int i = 0; i < weakSelf.termList.count; i ++) {
                HATermModel *mo = weakSelf.termList[i];
                mo.isSel = NO;
            }
            [weakSelf.tableView reloadData];
        };
    }
   
    if ([title isEqualToString:@"选择机具"]) {
        if (!self.selAgentModel) {
            [MBProgressHUD showError:@"请选择代理"];
            return;
        }
        [self loadNewData:self.selAgentModel.ID];
    }
}

-(void)setCellConWith:(UIView *)contentView title:(NSString *)title content:(NSString *)content isLine:(BOOL)isline isRImg:(BOOL)isRImg {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UILabel *titlabel = [UILabel ha_NLabelWithFont:17 textColor:@"#111a34" text:title alignment:0];
    [contentView addSubview:titlabel];
    [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(100);
    }];
    
    if (isRImg) {
        UIImageView *RImgV = [UIImageView ha_createImageView];
        RImgV.image = [UIImage imageNamed:@"ic_cell_more"];
        [contentView addSubview:RImgV];
        [RImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(5);
            make.height.mas_offset(10);
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-20);
        }];
    }
    
    UILabel *conLabel = [UILabel ha_NLabelWithFont:15 textColor:@"858b9c" text:content alignment:2];
    [contentView addSubview:conLabel];
    [conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(titlabel.mas_right).offset(15);
        if (isRImg) {
            make.right.mas_equalTo(-10-20-10);
        }else {
            make.right.mas_equalTo(-20);
        }
    }];
    
    if (isline == YES) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_offset(1);
            make.bottom.mas_equalTo(0);
        }];
    }
}


- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}

- (NSMutableArray *)termList
{
    if (!_termList) {
        _termList = [NSMutableArray array];
        
    }
    return _termList;
}


@end
